unit unGrafico;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, Series, DBChart, StdCtrls,
 Buttons, DB, TeeData, TeCanvas, TeeDBEdit, TeeDBCrossTab, ComCtrls;

type
 TfmGrafico = class(TForm)
  DataSource1: TDataSource;
  PageControl1: TPageControl;
  TabSheet1: TTabSheet;
  dbGrafico: TDBChart;
  Series2: TLineSeries;
  Series1: TLineSeries;
  Panel1: TPanel;
  btFechar: TBitBtn;
  btInserirDados: TBitBtn;
  btDesmarcar: TBitBtn;
  btMostrar: TBitBtn;
    Shape2: TShape;
    Label2: TLabel;
  procedure btFecharClick(Sender: TObject);
  procedure btInserirDadosClick(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure btDesmarcarClick(Sender: TObject);
  procedure btMostrarClick(Sender: TObject);
  procedure FormKeyPress(Sender: TObject; var Key: Char);
 private
    { Private declarations }
 public
    { Public declarations }
 end;
 
var
 fmGrafico: TfmGrafico;
 Serie: TLineSeries;
 
implementation

uses unDM;

{$R *.dfm}

procedure TfmGrafico.btInserirDadosClick(Sender: TObject);
var
 i: Integer;
begin
 
 dm.sqlTemp.Close;
 dm.sqlTemp.SQL.Clear;
 dm.sqlTemp.SQL.Text := 'select idpiloto,nome from ConsultaPontuacao';
 dm.sqlTemp.Open;
 
 dbGrafico.SeriesList.Clear;
 while not dm.sqlTemp.Eof do
  begin
   Serie := TLineSeries.Create(Self);
   Serie.Title := dm.sqlTemp.FieldByName('nome').AsString;
   Serie.Pen.Width := 2;
   Serie.Pointer.Style := psCircle;
   Serie.Pointer.Visible := True;
   Serie.Marks.Style := smsValue;

   dm.sqlAcumulado.Close;
   dm.sqlAcumulado.SQL.Clear;
   dm.sqlAcumulado.SQL.Text := 'select Posicao,(Acumulado-Penalidade) as Acumulado1 from Pontuacao where idpiloto=:idpiloto';
   dm.sqlAcumulado.Params[0].AsInteger := dm.sqlTemp.FieldByName('idpiloto').AsInteger;
   dm.sqlAcumulado.Open;

   i := 0;
   while not dm.sqlAcumulado.Eof do
    begin
     Serie.Add(dm.sqlAcumulado.FieldByName('Acumulado1').Value, IntToStr(i + 1));
     if (dm.sqlAcumulado.FieldByName('Posicao').AsInteger = 1) and (i < dm.sqlAcumulado.RecordCount) then
      Serie.ValueColor[i] := clWhite;
     dm.sqlAcumulado.Next;
     inc(i);
    end;


   dbGrafico.AddSeries(Serie);
   dm.sqlTemp.Next;

  end;
 dm.sqlAcumulado.Close;
 dm.sqlTemp.Close;
end;

procedure TfmGrafico.btDesmarcarClick(Sender: TObject);
var
 i: Integer;
begin

 i := 1;
 while i < dbGrafico.SeriesCount do
  begin
   if btDesmarcar.Caption = 'Desmarcar Todos' then
    dbGrafico.Series[i].Active := false
   else
    dbGrafico.Series[i].Active := true;
   inc(i);
  end;

 if btDesmarcar.Caption = 'Desmarcar Todos' then
  btDesmarcar.Caption := 'Marcar Todos'
 else
  btDesmarcar.Caption := 'Desmarcar Todos';


 
end;

procedure TfmGrafico.btMostrarClick(Sender: TObject);
var
 i: Integer;
begin

 if btMostrar.Caption = 'Mostrar Valores' then
  btMostrar.Caption := 'Ocultar Valores'
 else
  btMostrar.Caption := 'Mostrar Valores';

  i := 0;
 while i < dbGrafico.SeriesCount do
  begin
   if dbGrafico.Series[i].Marks.Visible then
    dbGrafico.Series[i].Marks.Visible := False
   else
    dbGrafico.Series[i].Marks.Visible := True;
   inc(i);
  end;
 
end;

procedure TfmGrafico.btFecharClick(Sender: TObject);
begin
 Close;
end;

procedure TfmGrafico.FormCreate(Sender: TObject);
begin
 btInserirDados.Click;
end;

procedure TfmGrafico.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key = #27 then
  Close;
end;

end.

